#! /usr/bin/env python

from PyQt4 import QtCore, QtGui
import PyQt4
import turing

try:
	_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
	def _fromUtf8(s):
		return s

try:
	_encoding = QtGui.QApplication.UnicodeUTF8
	def _translate(context, text, disambig):
		return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
	def _translate(context, text, disambig):
		return QtGui.QApplication.translate(context, text, disambig)

class QtList(QtCore.QAbstractListModel):
    def __init__(self, contents):
        super(QtList, self).__init__()
        self.contents = contents

    def rowCount(self, parent):
        return len(self.contents)

    def data(self, index, role):
        if role == QtCore.Qt.DisplayRole:
            return str(self.contents[index.row()])


class Ui_MainWindow(object):
	
	def __init__(self):
		self.tur = turing.Turing()
		self.nastro = []
		self.rawprog = ""
		self.sleeptime = 200
		self.mod = False
	
	def set_raw_prog(self,prog):
		self.tur.prog = []
		x = 0
		for line in prog.split("\n"):
			x = x + 1
			cmd = line.split(" ")
			if len(line) > 0:
				if len(cmd) == 5 and line[0] != "#":
					self.tur.prog.append(cmd)
				elif line[0] == "#":
					pass
				else:
					msgbox = QtGui.QMessageBox()
					msgbox.setText("Impossibile interpretare la linea "+str(x))
					msgbox.exec_()
		self.popola()
	
	def carica_file(self):
		fname = QtGui.QFileDialog.getOpenFileName()
		f = open(fname,"r")
		rawprog = "".join(f.readlines())
		self.set_raw_prog(rawprog)
		self.rawprog = rawprog


	def popola(self):
		items = []
		for l in self.tur.prog:
			items.append(" ".join(l))
		lst = QtList(items)
		self.listView.setModel(lst)
	
	def setnastro(self):
		self.tur.set_raw_nastro(str(self.lineEdit.text()).replace(" ","-"))

	def shownastro(self):
		x = 0
		for i in range(-4+self.tur.npos,5+self.tur.npos):
			self.nastro[x].setText(self.tur.getnastro(i))
			x = x + 1
	
	def start(self):
		self.setnastro()
		if len(self.tur.nastro) == 0 or len(self.tur.prog) == 0:
			msgbox = QtGui.QMessageBox()
			msgbox.setText("Impossibile avviare la macchina, deve essere caricato correttamente un programma ed un nastro di partenza")
			msgbox.exec_()
			return 0
		self.timer.start(self.sleeptime)
		self.pushButton_3.setEnabled(False)
		self.pushButton_4.setEnabled(True)
		self.dial.setEnabled(False)
		
	def stop(self):
		self.tur.reset()
		self.timer.stop()
		self.pushButton_3.setEnabled(True)
		self.pushButton_4.setEnabled(False)
		self.dial.setEnabled(True)
		
	def next(self):
		self.tur.next()
		self.shownastro()
		self.stato.setText(self.tur.stato)
		#self.listView.getItem(1).setSelected(True)
		index = self.listView.model().index(self.tur.ppos,0);
		self.listView.setCurrentIndex(index);
		if self.tur.stop:
			self.stop()
	
	def velocita(self):
		self.label_3.setText("Velocita: "+str(self.dial.value()))
		self.sleeptime = 1001-int(self.dial.value())
	
	def modifica(self):
		if self.mod == False:
			self.textEdit.setText(self.rawprog)
			self.textEdit.show()
			self.listView.hide()
			self.pushButton.setText("OK")
			self.pushButton_2.setEnabled(False)
			self.mod = True
			return 0
		if self.mod == True:
			self.rawprog = str(self.textEdit.toPlainText())
			self.set_raw_prog(self.rawprog)
			self.listView.show()
			self.textEdit.hide()
			self.pushButton.setText("Modifica")
			self.pushButton_2.setEnabled(True)
			self.mod = False
			return 0
	
	def salva(self):
		fname = QtGui.QFileDialog.getSaveFileName()
		f = open(fname,"w")
		f.writelines(self.rawprog)
		f.close()
		
	
	def setupUi(self, MainWindow):
		MainWindow.setObjectName(_fromUtf8("MainWindow"))
		MainWindow.resize(722, 616)
		font = QtGui.QFont()
		font.setPointSize(14)
		font.setBold(True)
		font.setWeight(75)
		MainWindow.setFont(font)
		self.centralwidget = QtGui.QWidget(MainWindow)
		self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
		self.label = QtGui.QLabel(self.centralwidget)
		self.label.setGeometry(QtCore.QRect(10, 10, 701, 41))
		font = QtGui.QFont()
		font.setPointSize(28)	
		font.setBold(True)
		font.setItalic(False)
		font.setWeight(75)
		self.label.setFont(font)
		self.label.setAlignment(QtCore.Qt.AlignCenter)
		self.label.setObjectName(_fromUtf8("label"))
		self.listView = QtGui.QListView(self.centralwidget)
		self.listView.setGeometry(QtCore.QRect(420, 80, 291, 361))
		self.listView.setObjectName(_fromUtf8("listView"))
		self.nastro_1 = QtGui.QLabel(self.centralwidget)
		self.nastro_1.setGeometry(QtCore.QRect(10, 300, 31, 31))
		self.nastro_1.setAutoFillBackground(False)
		self.nastro_1.setFrameShape(QtGui.QFrame.Box)
		self.nastro_1.setFrameShadow(QtGui.QFrame.Plain)
		self.nastro_1.setLineWidth(3)
		self.nastro_1.setAlignment(QtCore.Qt.AlignCenter)
		self.nastro_1.setObjectName(_fromUtf8("nastro_1"))
		self.nastro_2 = QtGui.QLabel(self.centralwidget)
		self.nastro_2.setGeometry(QtCore.QRect(50, 300, 31, 31))
		self.nastro_2.setAutoFillBackground(False)
		self.nastro_2.setFrameShape(QtGui.QFrame.Box)
		self.nastro_2.setFrameShadow(QtGui.QFrame.Plain)
		self.nastro_2.setLineWidth(3)
		self.nastro_2.setAlignment(QtCore.Qt.AlignCenter)
		self.nastro_2.setObjectName(_fromUtf8("nastro_2"))
		self.nastro_3 = QtGui.QLabel(self.centralwidget)
		self.nastro_3.setGeometry(QtCore.QRect(90, 300, 31, 31))
		self.nastro_3.setAutoFillBackground(False)
		self.nastro_3.setFrameShape(QtGui.QFrame.Box)
		self.nastro_3.setFrameShadow(QtGui.QFrame.Plain)
		self.nastro_3.setLineWidth(3)
		self.nastro_3.setAlignment(QtCore.Qt.AlignCenter)
		self.nastro_3.setObjectName(_fromUtf8("nastro_3"))
		self.nastro_4 = QtGui.QLabel(self.centralwidget)
		self.nastro_4.setGeometry(QtCore.QRect(130, 300, 31, 31))
		self.nastro_4.setAutoFillBackground(False)
		self.nastro_4.setFrameShape(QtGui.QFrame.Box)
		self.nastro_4.setFrameShadow(QtGui.QFrame.Plain)
		self.nastro_4.setLineWidth(3)
		self.nastro_4.setAlignment(QtCore.Qt.AlignCenter)
		self.nastro_4.setObjectName(_fromUtf8("nastro_4"))
		self.nastro_5 = QtGui.QLabel(self.centralwidget)
		self.nastro_5.setGeometry(QtCore.QRect(170, 290, 51, 51))
		font = QtGui.QFont()
		font.setPointSize(24)
		font.setBold(True)
		font.setItalic(False)
		font.setWeight(75)
		self.nastro_5.setFont(font)
		self.nastro_5.setAutoFillBackground(False)
		self.nastro_5.setFrameShape(QtGui.QFrame.Box)
		self.nastro_5.setFrameShadow(QtGui.QFrame.Plain)
		self.nastro_5.setLineWidth(1)
		self.nastro_5.setAlignment(QtCore.Qt.AlignCenter)
		self.nastro_5.setObjectName(_fromUtf8("nastro_5"))
		self.nastro_6 = QtGui.QLabel(self.centralwidget)
		self.nastro_6.setGeometry(QtCore.QRect(230, 300, 31, 31))
		self.nastro_6.setAutoFillBackground(False)
		self.nastro_6.setFrameShape(QtGui.QFrame.Box)
		self.nastro_6.setFrameShadow(QtGui.QFrame.Plain)
		self.nastro_6.setLineWidth(3)
		self.nastro_6.setAlignment(QtCore.Qt.AlignCenter)
		self.nastro_6.setObjectName(_fromUtf8("nastro_6"))
		self.nastro_7 = QtGui.QLabel(self.centralwidget)
		self.nastro_7.setGeometry(QtCore.QRect(270, 300, 31, 31))
		self.nastro_7.setAutoFillBackground(False)
		self.nastro_7.setFrameShape(QtGui.QFrame.Box)
		self.nastro_7.setFrameShadow(QtGui.QFrame.Plain)
		self.nastro_7.setLineWidth(3)
		self.nastro_7.setAlignment(QtCore.Qt.AlignCenter)
		self.nastro_7.setObjectName(_fromUtf8("nastro_7"))
		self.nastro_8 = QtGui.QLabel(self.centralwidget)
		self.nastro_8.setGeometry(QtCore.QRect(310, 300, 31, 31))
		self.nastro_8.setAutoFillBackground(False)
		self.nastro_8.setFrameShape(QtGui.QFrame.Box)
		self.nastro_8.setFrameShadow(QtGui.QFrame.Plain)
		self.nastro_8.setLineWidth(3)
		self.nastro_8.setAlignment(QtCore.Qt.AlignCenter)
		self.nastro_8.setObjectName(_fromUtf8("nastro_8"))
		self.nastro_9 = QtGui.QLabel(self.centralwidget)
		self.nastro_9.setGeometry(QtCore.QRect(350, 300, 31, 31))
		self.nastro_9.setAutoFillBackground(False)
		self.nastro_9.setFrameShape(QtGui.QFrame.Box)
		self.nastro_9.setFrameShadow(QtGui.QFrame.Plain)
		self.nastro_9.setLineWidth(3)
		self.nastro_9.setAlignment(QtCore.Qt.AlignCenter)
		self.nastro_9.setObjectName(_fromUtf8("nastro_9"))
		self.stato = QtGui.QLabel(self.centralwidget)
		self.stato.setGeometry(QtCore.QRect(150, 140, 101, 71))
		font = QtGui.QFont()
		font.setPointSize(20)
		font.setBold(True)
		font.setWeight(75)
		self.stato.setFont(font)
		self.stato.setFrameShape(QtGui.QFrame.Box)
		self.stato.setLineWidth(4)
		self.stato.setAlignment(QtCore.Qt.AlignCenter)
		self.stato.setObjectName(_fromUtf8("stato"))
		self.pushButton = QtGui.QPushButton(self.centralwidget)
		self.pushButton.setGeometry(QtCore.QRect(520, 460, 81, 27))
		font = QtGui.QFont()
		font.setPointSize(11)
		font.setBold(True)
		font.setWeight(75)
		self.pushButton.setFont(font)
		self.pushButton.setObjectName(_fromUtf8("pushButton"))
		self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
		self.pushButton_2.setGeometry(QtCore.QRect(430, 460, 81, 27))
		font = QtGui.QFont()
		font.setPointSize(11)
		self.pushButton_2.setFont(font)
		self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
		self.lineEdit = QtGui.QLineEdit(self.centralwidget)
		self.lineEdit.setGeometry(QtCore.QRect(0, 530, 521, 27))
		self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
		self.pushButton_3 = QtGui.QPushButton(self.centralwidget)
		self.pushButton_3.setGeometry(QtCore.QRect(540, 530, 81, 27))
		self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
		self.pushButton_4 = QtGui.QPushButton(self.centralwidget)
		self.pushButton_4.setGeometry(QtCore.QRect(640, 530, 71, 27))
		self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
		self.line = QtGui.QFrame(self.centralwidget)
		self.line.setGeometry(QtCore.QRect(0, 60, 741, 16))
		self.line.setFrameShape(QtGui.QFrame.HLine)
		self.line.setFrameShadow(QtGui.QFrame.Sunken)
		self.line.setObjectName(_fromUtf8("line"))
		self.line_2 = QtGui.QFrame(self.centralwidget)
		self.line_2.setGeometry(QtCore.QRect(0, 500, 721, 16))
		self.line_2.setFrameShape(QtGui.QFrame.HLine)
		self.line_2.setFrameShadow(QtGui.QFrame.Sunken)
		self.line_2.setObjectName(_fromUtf8("line_2"))
		self.line_3 = QtGui.QFrame(self.centralwidget)
		self.line_3.setGeometry(QtCore.QRect(0, 570, 721, 21))
		self.line_3.setFrameShape(QtGui.QFrame.HLine)
		self.line_3.setFrameShadow(QtGui.QFrame.Sunken)
		self.line_3.setObjectName(_fromUtf8("line_3"))
		self.label_2 = QtGui.QLabel(self.centralwidget)
		self.label_2.setGeometry(QtCore.QRect(0, 590, 721, 20))
		font = QtGui.QFont()
		font.setPointSize(11)
		font.setBold(False)
		font.setWeight(50)
		self.label_2.setFont(font)
		self.label_2.setAlignment(QtCore.Qt.AlignHCenter|QtCore.Qt.AlignTop)
		self.label_2.setObjectName(_fromUtf8("label_2"))
		self.pushButton_5 = QtGui.QPushButton(self.centralwidget)
		self.pushButton_5.setGeometry(QtCore.QRect(610, 460, 81, 27))
		font = QtGui.QFont()
		font.setPointSize(12)
		self.pushButton_5.setFont(font)
		self.pushButton_5.setObjectName(_fromUtf8("pushButton_5"))
		self.line_4 = QtGui.QFrame(self.centralwidget)
		self.line_4.setGeometry(QtCore.QRect(393, 70, 20, 431))
		self.line_4.setFrameShape(QtGui.QFrame.VLine)
		self.line_4.setFrameShadow(QtGui.QFrame.Sunken)
		self.line_4.setObjectName(_fromUtf8("line_4"))
		self.dial = QtGui.QDial(self.centralwidget)
		self.dial.setGeometry(QtCore.QRect(160, 380, 71, 81))
		self.dial.setMinimum(1)
		self.dial.setMaximum(1000)
		self.dial.setProperty("value", 900)
		self.dial.setObjectName(_fromUtf8("dial"))
		self.label_3 = QtGui.QLabel(self.centralwidget)
		self.label_3.setGeometry(QtCore.QRect(130, 470, 131, 17))
		self.label_3.setObjectName(_fromUtf8("label_3"))
		self.textEdit = QtGui.QTextEdit(self.centralwidget)
		self.textEdit.setGeometry(QtCore.QRect(420, 80, 291, 361))
		self.textEdit.setObjectName(_fromUtf8("textEdit"))
		MainWindow.setCentralWidget(self.centralwidget)
		
		
		self.textEdit.hide()
		self.pushButton_4.setEnabled(False)
		self.timer = QtCore.QTimer()

		self.retranslateUi(MainWindow)
		
		QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.modifica)
		QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL(_fromUtf8("clicked()")), self.carica_file)
		QtCore.QObject.connect(self.pushButton_3, QtCore.SIGNAL(_fromUtf8("clicked()")), self.start)
		QtCore.QObject.connect(self.pushButton_4, QtCore.SIGNAL(_fromUtf8("clicked()")), self.stop)
		QtCore.QObject.connect(self.pushButton_5, QtCore.SIGNAL(_fromUtf8("clicked()")), self.salva)
		QtCore.QObject.connect(self.dial, QtCore.SIGNAL(_fromUtf8("dialMoved(int)")), self.velocita)
		QtCore.QObject.connect(self.timer, QtCore.SIGNAL("timeout()"), self.next)
		
		
		
		QtCore.QMetaObject.connectSlotsByName(MainWindow)
		
		self.nastro.append(self.nastro_1)
		self.nastro.append(self.nastro_2)
		self.nastro.append(self.nastro_3)
		self.nastro.append(self.nastro_4)
		self.nastro.append(self.nastro_5)
		self.nastro.append(self.nastro_6)
		self.nastro.append(self.nastro_7)
		self.nastro.append(self.nastro_8)
		self.nastro.append(self.nastro_9)
		

	def retranslateUi(self, MainWindow):
		MainWindow.setWindowTitle(_translate("MainWindow", "GTur", None))
		self.label.setText(_translate("MainWindow", "GTur", None))
		self.nastro_1.setText(_translate("MainWindow", " ", None))
		self.nastro_2.setText(_translate("MainWindow", " ", None))
		self.nastro_3.setText(_translate("MainWindow", " ", None))
		self.nastro_4.setText(_translate("MainWindow", " ", None))
		self.nastro_5.setText(_translate("MainWindow", " ", None))
		self.nastro_6.setText(_translate("MainWindow", " ", None))
		self.nastro_7.setText(_translate("MainWindow", " ", None))
		self.nastro_8.setText(_translate("MainWindow", " ", None))
		self.nastro_9.setText(_translate("MainWindow", " ", None))
		self.stato.setText(_translate("MainWindow", "0", None))
		self.pushButton.setText(_translate("MainWindow", "Modifica", None))
		self.pushButton_2.setText(_translate("MainWindow", "Carica", None))
		self.pushButton_3.setText(_translate("MainWindow", "Start", None))
		self.pushButton_4.setText(_translate("MainWindow", "Stop", None))
		self.label_2.setText(_translate("MainWindow", "Galatolo Federico 2013", None))
		self.pushButton_5.setText(_translate("MainWindow", "Salva", None))
		self.label_3.setText(_translate("MainWindow", "Velocita: 900", None))

if __name__ == "__main__":
	import sys
	app = QtGui.QApplication(sys.argv)
	MainWindow = QtGui.QMainWindow()
	ui = Ui_MainWindow()
	ui.setupUi(MainWindow)
	MainWindow.show()
	sys.exit(app.exec_())

